import 'package:rxdart/rxdart.dart';

class CurAndPreData<T> {
  T current;
  T previous;

  CurAndPreData({required this.current, required this.previous});

  @override
  String toString() => 'pre: $previous, current: $current';
}

extension StreamExtension<T> on Stream<T> {
  Stream<CurAndPreData<T>> previous(T first) {
    return scan(
        (accumulated, value, index) =>
            CurAndPreData(current: value, previous: accumulated.current),
        CurAndPreData(current: first, previous: first));
  }

  Stream<T> filterDuplicates(T first) {
    return previous(first).where((event) {
      return event.previous != event.current;
    }).map((event) => event.current);
  }
}
